//
//  MZYthirdViewController.m
//  WristWatch
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYthirdViewController.h"
#import "MZYHeader.h"
#import "MZYthirdModel.h"
#import "MZYthirdTableViewCell.h"
#import "DataBaseHandle.h"
#import "MZYPriceModel.h"




@interface MZYthirdViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *array;
@property(nonatomic, retain)MBProgressHUD *HUD;
@property(nonatomic, retain)NSArray *gallary;// 图片个数
@property(nonatomic, retain)NSMutableArray *picArr;// 图片数组
@property(nonatomic, retain)NSMutableArray *mktArr;
@property(nonatomic, retain)NSMutableArray *listArr;
@property(nonatomic, retain)NSMutableArray *listArrSecond;// 外观数组


@property(nonatomic, retain)NSDictionary *tableDic;
@property(nonatomic, retain)NSMutableArray *dictable1;
@property(nonatomic, retain)NSMutableDictionary *dictable2;
@property(nonatomic, retain)NSMutableDictionary *dictable3;
@end

@implementation MZYthirdViewController

- (void)dealloc {
    [_tableView release];
    [_array release];
    [_HUD release];
    [_gallary release];
    [_picArr release];
    [_mktArr release];
    [_listArr release];
    [_listArrSecond release];
    
    [_tableDic release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.picArr = [NSMutableArray array];
        self.mktArr = [NSMutableArray array];
        self.listArr = [NSMutableArray array];
        self.listArrSecond = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    
    [self creeatTableView];
    [self getData];
    
}


#pragma mark- 创建tableView
- (void)creeatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    // 建立小菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.detailsLabelText = @"请稍后";
    self.HUD.dimBackground = YES;
    
#pragma mark- 收藏
    //    self.view.backgroundColor = [UIColor whiteColor];
    [[DataBaseHandle shareDataBaseHandle] createTable];
    // 用一个枚举类型的变量接受结果, 变量去比较
    NSLog(@"111111111%@",self.style);
    selectStyle result = [[DataBaseHandle shareDataBaseHandle] isHaveInTable:self.style];
    if (result == InTable) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"14"] style:UIBarButtonItemStylePlain target:self action:@selector(saveStyle:)];
    } else if (result == NotInTable) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"13"] style:UIBarButtonItemStylePlain target:self action:@selector(saveStyle:)];
    } else {
        NSLog(@"出错");
    }
    
    
    [[DataBaseHandle shareDataBaseHandle] openDB];
    
    
}



//- (void)saveStyle:(UIBarButtonItem *)button {
//    if ([button.title isEqualToString:@"收藏"]) {
//        [[DataBaseHandle shareDataBaseHandle] saveStyleInDB:self.style];
//        button.title = @"已收藏";
//    } else {
//        [[DataBaseHandle shareDataBaseHandle] deleteStyleInDB:self.style];
//        button.title = @"收藏";
//    }
//}

- (void)saveStyle:(UIBarButtonItem *)button {
    if ([button.image isEqual:[UIImage imageNamed:@"13"]]) {
        [[DataBaseHandle shareDataBaseHandle] saveStyleInDB:self.style];
        button.image = [UIImage imageNamed:@"14"];
    } else {
        [[DataBaseHandle shareDataBaseHandle] deleteStyleInDB:self.style];
        button.image = [UIImage imageNamed:@"13"];
    }
}



#pragma mark -创建头部图片
- (void)creatHeaderView {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 2)];
//    header.backgroundColor = [UIColor orangeColor];
    self.tableView.tableHeaderView = header;
    
    
    UIScrollView *scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, header.frame.size.height)];
    //    scrollerView.contentOffset = CGPointMake(WIDTH, 0);
    scrollerView.pagingEnabled = YES;
    scrollerView.showsHorizontalScrollIndicator = NO;
    scrollerView.contentSize = CGSizeMake(self.gallary.count * WIDTH, 0);
    [header addSubview:scrollerView];
    
    
    for (NSInteger i = 0; i < self.gallary.count; i ++) {
        MZYPriceModel *model = self.picArr[0];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, header.frame.size.height)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.gallary[i]] placeholderImage:nil];
        NSLog(@"111222:%@",model.gallary[i]);
//        imageView.backgroundColor = [UIColor blueColor];
        [scrollerView addSubview:imageView];
        [imageView release];
        
        UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake((imageView.frame.size.width - 60) , header.frame.size.height - header.frame.size.height / 6, 50, 50)];
        countLabel.font = [UIFont boldSystemFontOfSize:15];
        countLabel.textAlignment = 1;
        countLabel.text = [NSString stringWithFormat:@"%ld%@%ld",(long)i + 1,@"/",(unsigned long)self.gallary.count];
//        countLabel.backgroundColor = [UIColor colorWithRed:1.000 green:0.500 blue:0.000 alpha:0.750];
        countLabel.layer.cornerRadius = 25;
        countLabel.clipsToBounds = YES;
        [imageView addSubview:countLabel];
        [countLabel release];
        
        
    }
    
}

#pragma mark 创建推荐品牌
- (void)creatbrandView {
    
    
}

#pragma mark - tableView返回个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    if (section == 0) {
//        return 1;
//    } else
    if (section == 0) {
        return self.listArr.count;
    } else {
        return self.listArrSecond.count;
    }
}

#pragma mark -tableView分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

#pragma mark -tableView重用池
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    if (indexPath.section == 0) {
//        static NSString *reuse = @"Cell";
///        LYLPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
////        if (cell == nil) {
////            cell = [[LYLPriceCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
////        }
////        
////        cell.downLabel.text =[NSString stringWithFormat:@"%@%@",@"中国零售价:", self.price];
////        cell.upLabel.text = [NSString stringWithFormat:@"%@",self.name];
////        
//        return cell;
    if (indexPath.section == 0) {
        static NSString *reuse = @"CellTwo";
        MZYthirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (cell == nil) {
            cell = [[MZYthirdTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        MZYthirdModel *model = self.listArr[indexPath.row];
        cell.leftLabel.text = model.key;
        cell.rightLabel.text = model.value;
        
        
        return cell;
    } else {
        static NSString *reuse = @"CellThird";
        MZYthirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (cell == nil) {
            cell = [[MZYthirdTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        MZYthirdModel *model = self.listArrSecond[indexPath.row];
        cell.leftLabel.text = model.key;
        cell.rightLabel.text = model.value;
        return cell;
    }
}



#pragma mark - 网络请求
- (void)getData {
    NSString *str = [NSString stringWithFormat:@"http://api.ttime.com/productApi/productDetails?token=f1cfa3eafd64a3474df1d5567d830da6&product_id=%@&v=2.3.0&cityid=1912&uid=0",self.style.product_id];
    NSLog(@"1111116666699999%@",str);

    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = responseObject;
        
        NSDictionary *resultDic = dic[@"result"];
        self.gallary = resultDic[@"gallary"];
        MZYPriceModel *model = [[MZYPriceModel alloc] init];
        [model setValuesForKeysWithDictionary:resultDic];
        [self.picArr addObject:model];
        [model release];
        
        
        NSArray *mktArr = resultDic[@"mkt_prices"];
        for (NSDictionary *mktDic in mktArr) {
            MZYthirdModel * model = [[MZYthirdModel alloc] init];
            [model setValuesForKeysWithDictionary:mktDic];
            [self.mktArr addObject:model];
        }
        
        NSArray *parameterArr = resultDic[@"parameter"];
        NSDictionary *dic0 = parameterArr[0];
        NSArray *listArr = dic0[@"list"];
        for (NSDictionary *dicList in listArr) {
            MZYthirdModel *model = [[MZYthirdModel alloc] init];
            [model setValuesForKeysWithDictionary:dicList];
            [self.listArr addObject:model];
            [model release];
        }
        
        NSDictionary *dic1 = parameterArr[1];
        NSArray *listSecondArr = dic1[@"list"];
        for (NSDictionary *dicList in listSecondArr) {
            MZYthirdModel *model = [[MZYthirdModel alloc] init];
            [model setValuesForKeysWithDictionary:dicList];
            [self.listArrSecond addObject:model];
            [model release];
        }
        
        if (self.picArr.count > 0) {
            self.HUD.hidden = YES;
            [self.tableView reloadData];
            [self creatHeaderView];
        }
        [self creatHeaderView];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

#pragma mark - 返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.section == 0) {
//        return 100;
//    } else
    if (indexPath.section == 0) {
        MZYthirdModel *model = self.listArr[indexPath.row];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13], NSFontAttributeName,nil];
        
        CGRect rect = [model.value boundingRectWithSize:CGSizeMake(WIDTH / 1.8, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        if (rect.size.height > 40) {
            return 40 + rect.size.height - 40;
        } else {
            return 50;
        }
    } else {
        MZYthirdModel *model = self.listArrSecond[indexPath.row];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13], NSFontAttributeName,nil];
        
        CGRect rect = [model.value boundingRectWithSize:CGSizeMake(WIDTH / 1.8, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        if (rect.size.height > 40) {
            return 40 + rect.size.height - 40;
        } else {
            return 50;
        }
    }
}

#pragma mark -分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 1) {
        UIView *viewOne = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH , 100)];
        viewOne.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:viewOne];
        [viewOne release];
        
        UILabel *labelOne = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, WIDTH / 4, 20)];
        labelOne.text = @"基本信息";
        [viewOne addSubview:labelOne];
        [labelOne release];
        
        return viewOne;
    } else {
        UIView *viewtwo = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH , 100)];
        viewtwo.backgroundColor = [UIColor brownColor];
        viewtwo.alpha = 0.8;
        [self.view addSubview:viewtwo];
        [viewtwo release];
        
        UILabel *labelTwo = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, WIDTH / 4, 20)];
        labelTwo.text = @"其他";
        [viewtwo addSubview:labelTwo];
        [labelTwo release];
        
        return viewtwo;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
